package agent;

import common.Piece;
import common.DominoState;
import java.util.Iterator;
import java.util.LinkedList;

public class AgentState extends DominoState {

    /**
     * List of the agent's pieces.
     */
    protected LinkedList<Piece> agentPieces;
    //peçasPossiveisDoAdversario
    
    public LinkedList<Piece> opponentPossiblePieces;
    protected Player lastPlayer = Player.NONE;

    public AgentState() {
        super();
        opponentPossiblePieces = new LinkedList<>();
        agentPieces = new LinkedList<>();

    }

    public AgentState(LinkedList<Piece> agentPieces, LinkedList<Piece> playedPieces) {
        if (playedPieces != null) {
            this.playedPieces = playedPieces;
        }
        this.agentPieces = new LinkedList<>();
        for (Piece piece : agentPieces) {
            this.agentPieces.add(piece);
        }
        opponentPossiblePieces = new LinkedList<>();
        for (Piece piece : allPieces) {
            if (!agentPieces.contains(piece)) {
                opponentPossiblePieces.add(piece);
            }
        }

    }

    public AgentState(DominoState ds) {
        super(ds);
        opponentPossiblePieces = new LinkedList<>();
        agentPieces = new LinkedList<>();
    }

    public void recalcOpponentPossiblePiecesBasedOnPlayerSkip(byte leftValue, byte rightValue) {
        for (Iterator<Piece> it = opponentPossiblePieces.iterator(); it.hasNext();) {
            Piece p = it.next();
            //XXX porque iterator e nao for? com foreach ao remover as posicoes iam mudar e dava coco
            if (p.getValue1() == leftValue || p.getValue1() == rightValue
                    || p.getValue2() == leftValue || p.getValue2() == rightValue)
            {
                it.remove();
            }
        }
    }

    public int getAllDotsPlayer(LinkedList<Piece> playerList) {
        int soma = 0;
        for (Piece p : playerList) {
            soma += p.getValue1() + p.getValue2();
        }
        return soma;
    }


}
